﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;
using Cat;
using System.Data.Odbc;

namespace Thesis_2
{
    public partial class frmProductPrice : Form
    {
        DatabaseConnection dbcon = new DatabaseConnection();
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        public frmProductPrice()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmProductPrice_Load);
            // Tab1
            btnclear.Click += new EventHandler(btnclear_Click);
            txtsearch.TextChanged += new EventHandler(txtsearchuser_TextChanged);
            txtSRP.KeyPress += new KeyPressEventHandler(txtSRP_KeyPress);
            dataGridView2.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView2_RowHeaderMouseClick);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtprodID.Text == "")
            {
                    MessageBox.Show("Please Search Product ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtsearch.Focus();
                    return;
            }
            if (txtSRP.Text == "")
            {
                MessageBox.Show("Please Enter Suggested retail price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSRP.Focus();
                return;
            }
            dbcon.dbconnection();
            dbcon.UpdatePrice("Update tblprice set tblprice.SuggestedPrice = '" + txtSRP.Text + "' Where tblprice.ProductID ='" + txtprodID.Text + "';");
            MessageBox.Show("Update Success!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dbcon.DBClose();
            getprice();
            reset();
        }

        void btnclear_Click(object sender, EventArgs e)
        {
            reset();
        }
        void txtSRP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        //Tab1     
        void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow dr = dataGridView2.SelectedRows[0];
            txtprodID.Text = dr.Cells[0].Value.ToString();
            txtSRP.Text = dr.Cells[2].Value.ToString();

        }
        void txtsearchuser_TextChanged(object sender, EventArgs e)
        {
            if (txtsearch.Text != "Search Product")
            {
                dbcon.dbconnection();
               // dbcon.loadData("SELECT  * FROM tblprice WHERE ProductName or ProductID LIKE '%" + txtsearch.Text + "%'");
                dbcon.loadData("Select tblprice.ProductID, tblproduct.ProductName ,tblprice.SuggestedPrice,tblprice.Dateadded from tblprice INNER JOIN tblproduct ON tblprice.ProductID=tblproduct.ProductID WHERE tblprice.ProductID  OR tblproduct.ProductName LIKE '%" + txtsearch.Text+"%'");
                dataGridView2.DataSource = dbcon.tbl;
                dbcon.DBClose();
            }
            else
            {
                getprice();
            }
        }
        //Load and reset
        void frmProductPrice_Load(object sender, EventArgs e)
        {
            dataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            getprice();
            
        }
        void reset()
        {
            txtprodID.Text = "";
            txtSRP.Text = "";
        }
        void getprice()
        {

            dbcon.dbconnection();
            dbcon.loadData("Select tblprice.ProductID, tblproduct.ProductName ,tblprice.SuggestedPrice,tblprice.Dateadded from tblprice INNER JOIN tblproduct ON tblprice.ProductID=tblproduct.ProductID");
            dataGridView2.DataSource = dbcon.tbl;
            dbcon.DBClose();
            dataGridView2.Columns[2].DefaultCellStyle.Format = "#,#0.00";
            dataGridView2.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }
        


    }
}
